﻿package core.ui.dialogs
{
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;

    public class LoginDialog extends Dialog
    {
        var email:TextInput;
        var password:TextInput;
        var message:Literal;
        var remember:Checkbox;
        var doafterlogin:Function;
        var doaftercancel:Function;

        public function LoginDialog(param1:Tuyitu, param2:Function = null, param3:Function = null)
        {
            var _loc_4:Literal = null;
            super(param1, "rm_login", 240, 245, true);
            this.doafterlogin = param2;
            this.doaftercancel = param3;
            _loc_4 = new Literal("用户名");
            _loc_4.x = 18;
            _loc_4.y = 30;
            addChild(_loc_4);
            this.email = new TextInput(200, 800, "");
            this.email.value = U.GetLocalData("email");
            this.email.x = 20;
            this.email.y = 47;
            addChild(this.email);
            _loc_4 = new Literal("rm_password");
            _loc_4.x = 18;
            _loc_4.y = 75;
            addChild(_loc_4);
            this.password = new TextInput(200, 800, "");
            this.password.t.displayAsPassword = true;
            this.password.x = 20;
            this.password.y = 92;
            addChild(this.password);
            this.remember = new Checkbox("rm_remember_me");
            this.remember.x = 20;
            this.remember.y = 122;
            addChild(this.remember);
            var _loc_5:* = new IconLink(new AddIcon(), "rm_register_account");
            _loc_5.addEventListener(MouseEvent.CLICK, this.RegisterClick, false, 1, true);
            _loc_5.x = 20;
            _loc_5.y = 150;
            addChild(_loc_5);
            var _loc_6:* = new IconLink(new LostIcon(), "rm_lost_password");
            _loc_6.addEventListener(MouseEvent.CLICK, this.LostPasswordClick, false, 1, true);
            _loc_6.x = 20;
            _loc_6.y = 172;
            addChild(_loc_6);
            this.message = new Literal("rm_working", true);
            this.message.x = 25;
            this.message.y = 205;
            this.message.visible = false;
            this.message.width = 200;
            addChild(this.message);
            param1.stage.focus = this.email.value != "" ? (this.password.t) : (this.email.t);
            return;
        }// end function

        private function RegisterClick(event:MouseEvent) : void
        {
            Close();
            ed.cm.Register();
            return;
        }// end function

        private function LostPasswordClick(event:MouseEvent) : void
        {
            Close();
            ed.cm.LostPassword();
            return;
        }// end function

        override public function CloseClick(event:MouseEvent) : void
        {
            Close();
            if (this.doaftercancel != null)
            {
                this.doaftercancel();
            }
            else
            {
                ed.CheckToShowSplash();
            }
            return;
        }// end function

        override public function OKClick(event:MouseEvent) : void
        {
            if (this.email.value == "" || this.password.value == "")
            {
                new AlertDialog(ed, "rm_insert_data");
            }
            else
            {
                var _loc_2:Boolean = false;
                cancel.visible = false;
                ok.visible = _loc_2;
                this.message.visible = true;
                U.CallHome("login", {email:this.email.value, password:this.password.value, remember:this.remember.checked}, this.Callback);
            }
            return;
        }// end function

        public function Callback(CLICK:Object) : void
        {
            U.SetLocalData("email", this.email.text);
            var _loc_2:Boolean = true;
            cancel.visible = true;
            ok.visible = _loc_2;
            this.message.visible = false;
            if (CLICK.success)
            {
                U.Log("rm_login");
                ed.cm.LoginUser({name:CLICK.payload.name, email:CLICK.payload.email});
                Close();
                if (this.doafterlogin != null)
                {
                    this.doafterlogin();
                }
            }
            else
            {
                switch(String(CLICK.payload).toLowerCase())
                {
                    case "err:login_fail":
                    {
                        new AlertDialog(ed, "rm_login_fail");
                        break;
                    }
                    default:
                    {
                        new AlertDialog(ed, "rm_error");
                        break;
                    }
                }
                this.password.value = "";
                ed.stage.focus = this.password.t;
            }
            return;
        }// end function

    }
}
